﻿# -*- coding: utf-8 -*-

from price import *
import re

class DNSShop(PriceList):
    """ Прайс-лист магазина DNS """
    
    def __init__(self):
        self.prefix = "http://www.dns-shop.ru/samara/price.php?c=" 
        self.details = "http://www.dns-shop.ru/samara/price_detail.php?i="
        self.list = {}
        self.categories = {
            "Proc"  : "7000007",
            "MBoard": "7000008",
            "HDD"   : "7000029",
            "CD-ROM": "7000030",
            "Box"   : "7000031",
            "Video" : "7000009",
            "Lan"   : "7000125",
            "LCD"   : "7000033",
            "Other" : "7000034",
            "Power" : "7000038"
        }
        
    def get_category(self,category):
        items = []
        link = self.prefix + self.categories[category]
        page = self.get_page(link,"windows-1251")
        items_re = re.finditer(ur"d\([^);]+\);",page)
        for i in items_re:
            #Извлечение информации по одному элементу
            dx = i.group()
            dots = dx.split(",")
            it_re = re.finditer(ur"\"[^\"]+\"",dx)
            l = []
            for j in it_re:
                t = j.group()
                l.append(t[1:len(t)-1])
            if (len(l) > 1):
                item = PriceItem()
                item.category = category
                item.description = l[0]
                try:
                    #разобрать описание
                    price = l[1].replace(" ","")
                    item.price = int(price)
                except:
                    pass
                item.details_url = dots[len(dots)-3]
                items.append(item)
        return items